// 引入接口文件
import { getList,getDetail,queryRecommendAndDealNum,exportList} from './intermediaryApplyListServ'
import Config from '../../../config/config'
// 对象和JSON字符串互转
import { parse ,stringify} from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import config from 'config/config';
import { download } from 'utils/util'

// 中介公司名  报备客户姓名   报备客户电话 报备客户状态
const queryList = [
  {name: '公司名称', type: 'text', key: 'orgName', value: '',placeholder:'支持模糊搜索'},
  {name: '申请合作时间', type: 'time', key: 'time', value: '',placeholder:''},

]

// 初始默认状态
const defultState = {
  exportLoading: false,
  loading: false,
  btnLoading: false,
  queryList,
  statisticsInfo:{
    dealNum: 0,//成交数
    recommendNum: 0,//报备数
  },//统计信息
  currentItem: {},
  modalObj:{
    visible:false,
  },
  productSelf: [],
  activeKey: '1',
  // 表格数据
  tableData: {
    dataSource: [
    ],
    total: 0,
  },
  // 表格分页设置
  pageConfig: {
    pageSize: Number.parseInt(config.smallPageSizeList[0]),
    pageNo: 1
  },

}

// 声明module
const tmpModule = {
  // namespace: 'productListModel',
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          // dispatch({ type: 'updateStore', payload: {...cloneDeep(defultState), ...query} });//每次进去页面先重置状态
          // 执行业务操作
          dispatch({type: 'search',payload: {} }) // end dispatch
          // dispatch({type: 'queryRecommendAndDealNum',payload: {} }) // 查询统计信息
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    //查询操作
    * search ({payload = {}}, {call, put, select}) {
      let {queryList, tableData, pageConfig} = yield select(state => state[tmpModule.namespace])
      const pageSize = pageConfig.pageSize
      //显示loading状态
      yield put({type: 'updateStore', payload: {loading: true, btnLoading: true, tableData: {total: 0},}})
    
      //组装接口入参
      let params = {
        
      }
      let query={}
      //console.log(queryList)
      queryList.map(item => {
        // if(item.key=='status'){
        //   item.value=Number(item.value)
        // }
        query[item.key] = item.value
      })
      console.log(222,query)
      params.orgName=query.orgName
      params.createStartTime=query.time[0]
      params.createEndTime=query.time[1]
      const {rows, success, msg, total, pageNo} = yield call(getList, {
        ...params,
        pageNo: payload.pageNo || 1,
        pageSize,
      })
      yield put({type: 'updateStore', payload: {loading: false, btnLoading: false}})
      //console.log('数据', rows, success, msg, total, pageNo)
      if (!success) {
        message.error(msg)
        return false
      }
      let list = rows||[]
      list.map((item, index) => {
        item.key = index + 1
      })
      tableData = {
        dataSource: list,
        total: Number(total)
      }
      yield put({
        type: 'updateStore', payload: {
          loading: false,
          btnLoading: false,
          pageConfig: {...pageConfig, ...payload},
          tableData,
        }
      })
    },
    *pageChange({ payload }, { put, select }) {
      const { pageNo, pageSize } = payload;
      const { pageConfig } =yield select(state => state[tmpModule.namespace])
      yield put({ type: 'updateStore', payload: { pageConfig: { ...pageConfig, ...payload } } });
      yield put({ type: 'search', payload: { pageNo, pageSize } });
    },
    // 查询条件重置
    *queryReset({ }, { put }) {
      yield put({ type: 'updateStore', payload: { queryList: cloneDeep(queryList) } });
      // yield put({ type: 'search' })
    },
    //改变查询条件
    *inputChange({ payload }, { call, put, select }) {
      let { queryList } = yield select(state => state[tmpModule.namespace])
      const { key, value } = payload;
      let data = queryList.find(item => item.key === key);
      data.value = value;
      //console.log(1111,queryList)
      yield put({ type: 'updateStore', payload: { queryList: [...queryList, ...data] } });
    },
    
    //导出
    * exportList ({payload}, {put, call, select}) {
      
      let {queryList,exportLoading, orgId, agentConfigId} = yield select(state => state[tmpModule.namespace])
      //组装接口入参
      let params = {
        pageSize:100000,
        pageNo:1,
      }
      
      queryList.map(item => {
        // if(item.key=='status'){
        //   item.value=Number(item.value)
        // }
        params[item.key] = item.value
      })
      
      yield put({type: 'updateStore', payload: {exportLoading:true}})
      // let url=`${Config.baseUrl}admin/cooper/export?${stringify(params)}`
      const {extParams, success, msg, total, pageNo} = yield call(exportList, params)
      if (success) {
        download(extParams.href)
      }
      yield put({type: 'updateStore', payload: {exportLoading:false}})
    },
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
